import tkinter as tk
from pynput import keyboard
import threading


# 定义回调函数
def on_activate_h():
  print("热键 Ctrl+Alt+H 被触发了！")


# 定义热键组合
hotkey = {keyboard.Key.ctrl_l, keyboard.Key.alt_l, 'h'}


# 监听热键事件
def on_press(key):
  if hasattr(key, 'vk'):
    print('char=', key.vk)
    print('key=', key, type(key))
    print('key=', key.combining)
    print('key=', key._parameters)
    print('key=', dir(key))
  else:

    print('key=', key, type(key), key.name, key.value)
  if all(k in pressed_keys for k in hotkey):
    on_activate_h()


def on_release(key):
  try:
    pressed_keys.remove(key.char)
  except KeyError:
    pressed_keys.discard(key)


pressed_keys = set()


def start_key_listener():
  with keyboard.Listener(on_press=on_press) as listener:
    listener.join()


# 创建 Tkinter 窗口
def create_gui():
  root = tk.Tk()
  root.title("Tkinter + Pynput")

  label = tk.Label(root, text="按下 Ctrl+Alt+H 来触发热键")
  label.pack(padx=20, pady=20)

  # 运行 Tkinter 主事件循环
  root.mainloop()


# 启动键盘监听器线程
listener_thread = threading.Thread(target=start_key_listener, daemon=True)
listener_thread.start()

# 启动 Tkinter GUI
create_gui()
